﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BattleOfColors.Control;

namespace BattleOfColors.Model
{
    public enum SmellType
    {
        GREEN_SOLDIER, 
        GREEN_PAIN,
        

        RED_SOLDIER,
        RED_PAIN,

        ITEM
    }

    /// <summary>
    /// Essa classe é responsável pelo cheiro que os soldados deixam no mapa
    /// </summary>
    public class Smell : IComparable
    {
        private int _soldierIdentifier;

        public int SoldierIdentifier
        {
            get { return _soldierIdentifier; }
            set { _soldierIdentifier = value; }
        }

        private int _remaingTime;

        public int RemaingTime
        {
            get { return _remaingTime; }
            set { _remaingTime = value; }
        }

        private SmellType _type;

        public SmellType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        /// <summary>
        /// Construtor default de um cheiro.
        /// O tempo remanescente é determinado pelo tipo do cheiro.
        /// </summary>
        /// <param name="Type"></param>
        public Smell(SmellType Type)
        {
            this.Type = Type;

            switch (Type)
            {
                case SmellType.GREEN_SOLDIER:
                    RemaingTime = Specification.Instance.DefaultSoldierSmellTime;
                    break;
                case SmellType.RED_SOLDIER:
                    RemaingTime = Specification.Instance.DefaultSoldierSmellTime;
                    break;
                case SmellType.GREEN_PAIN:
                    RemaingTime = Specification.Instance.DefaultSoldierBloodSmellTime;
                    break;
                case SmellType.RED_PAIN:
                    RemaingTime = Specification.Instance.DefaultSoldierBloodSmellTime;
                    break;
                case SmellType.ITEM:
                    RemaingTime = Specification.Instance.DefaultItemSmellTime;
                    break;
                default:
                    RemaingTime = Specification.Instance.DefaultSmellTime;
                    break;
            }

            this.SoldierIdentifier = -1;
        }

        /// <summary>
        /// Construtor para dado um soldado, criar um cheiro específico de sua cor
        /// </summary>
        /// <param name="soldier"></param>
        public Smell(Soldier soldier)
        {
            if (soldier.Color == Color.GREEN)
            {
                if (soldier.Life == Specification.Instance.DefaultLife)
                {
                    Type = SmellType.GREEN_SOLDIER;
                    RemaingTime = Specification.Instance.DefaultSoldierSmellTime;
                }
                else
                {
                    Type = SmellType.GREEN_PAIN;
                    RemaingTime = Specification.Instance.DefaultSoldierBloodSmellTime;
                }
            }
            else
            {
                if (soldier.Life == Specification.Instance.DefaultLife)
                {
                    Type = SmellType.RED_SOLDIER;
                    RemaingTime = Specification.Instance.DefaultSoldierSmellTime;
                }
                else
                {
                    Type = SmellType.RED_PAIN;
                    RemaingTime = Specification.Instance.DefaultSoldierBloodSmellTime;
                }
            }

            SoldierIdentifier = soldier.Identifier;
        }
        
        /// <summary>
        /// Método para dado um tipo de cheiro, retornar a sua cor no mapa
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static System.Drawing.Color GetSmellColor(SmellType type)
        {
            System.Drawing.Color color = System.Drawing.Color.Black;

            switch (type)
            {
                case SmellType.GREEN_SOLDIER:
                    color = System.Drawing.Color.GreenYellow;
                    break;
                case SmellType.RED_SOLDIER:
                    color = System.Drawing.Color.Pink;
                    break;
                case SmellType.GREEN_PAIN:
                    color = System.Drawing.Color.LightSkyBlue;
                    break;
                case SmellType.RED_PAIN:
                    color = System.Drawing.Color.Salmon;
                    break;
                case SmellType.ITEM:
                    color = System.Drawing.Color.WhiteSmoke;
                    break;
                default:
                    break;
            }

            return color;
        }

        public void Update()
        {
            RemaingTime--;
        }

        public bool IsBloodSmell()
        {
            return Type == SmellType.GREEN_PAIN || Type == SmellType.RED_PAIN;
        }

        /// <summary>
        /// Retorna caso o tempo remanescente do cheiro ainda é maior que 0 (zero)
        /// </summary>
        /// <returns></returns>
        public bool StillUp()
        {
            return RemaingTime > 0;
        }

        /// <summary>
        /// Método estático para retornar um cheiro de sangue dado um inimigo ferido
        /// </summary>
        /// <param name="woundedSoldier"></param>
        /// <returns></returns>
        public static Smell BloodSmell(Soldier woundedSoldier)
        {
            if(woundedSoldier.Color == Color.GREEN)
            {
                return new Smell(SmellType.GREEN_PAIN);
            }
            else
            {
                return new Smell(SmellType.RED_PAIN);
            }
        }

        /// <summary>
        /// Método para realizar cópia profunda de um cheiro
        /// </summary>
        /// <returns></returns>
        public Smell CloneSmell()
        {
            Smell smell = new Smell(Type);
            smell.RemaingTime = RemaingTime;
            return smell;
        }

        /// <summary>
        /// Método para a comparação de dois cheiros pela quantidade de tempo remanescente. 
        /// Quanto maior o tempo remanescente, mais à frente será o cheiro.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return 1;
            }

            if (obj is Smell)
            {
                Smell anotherSmell = (Smell)obj;

                if (anotherSmell.RemaingTime > RemaingTime)
                {
                    return -1;
                }
                else
                {
                    return 1;
                }
            }
            else
            {
                return 1;
            }
        }
    }
}

